import pygame
import datetime
import sys
import os
import argparse

# 设置显示环境变量
os.environ['DISPLAY'] = ':0'
os.environ['XAUTHORITY'] = '/home/w-dingyu01@uos/.Xauthority'

class SimpleWallpaperGenerator:
    def __init__(self, output_path, width=1920, height=1080):
        self.output_path = output_path
        self.width = width
        self.height = height
        self.path = '/home/w-dingyu01@uos/dy外网/My_model/pygame测试/'
        
        # 初始化pygame
        pygame.init()
        self.surface = pygame.Surface((width, height))
        
        # 定义颜色
        self.COLORS = {
            'BACKGROUND': (30, 30, 60),
            'TEXT': (255, 255, 255),
            'ACCENT': (0, 120, 255),
            'SHADOW': (50, 50, 50),
            'SECONDARY': (173, 216, 230)
        }
        
        self.load_fonts()

    def load_fonts(self):
        """加载字体"""
        try:
            self.title_font = pygame.font.Font(os.path.join(self.path, 'Ubuntu_18.04_SimHei.ttf'), 48)
            self.content_font = pygame.font.Font(os.path.join(self.path, 'Ubuntu_18.04_SimHei.ttf'), 32)
        except:
            self.title_font = pygame.font.Font(None, 48)
            self.content_font = pygame.font.Font(None, 32)

    def draw_background(self):
        """绘制简约背景"""
        # 纯色背景
        self.surface.fill(self.COLORS['BACKGROUND'])
        
        # 添加简单的网格线
        grid_color = (40, 40, 80)
        for x in range(0, self.width, 100):
            pygame.draw.line(self.surface, grid_color, (x, 0), (x, self.height), 1)
        for y in range(0, self.height, 100):
            pygame.draw.line(self.surface, grid_color, (0, y), (self.width, y), 1)

    def draw_text_content(self, content):
        """绘制文本内容，支持换行"""
        lines = content.split('\n')
        
        # 计算起始Y位置（垂直居中）
        total_height = len(lines) * 50
        start_y = (self.height - total_height) // 2
        
        for i, line in enumerate(lines):
            if line.strip():  # 跳过空行
                # 绘制文字阴影
                shadow = self.content_font.render(line, True, self.COLORS['SHADOW'])
                shadow_rect = shadow.get_rect(center=(self.width/2 + 2, start_y + i*50 + 2))
                self.surface.blit(shadow, shadow_rect)
                
                # 绘制文字
                text = self.content_font.render(line, True, self.COLORS['TEXT'])
                text_rect = text.get_rect(center=(self.width/2, start_y + i*50))
                self.surface.blit(text, text_rect)

    def draw_header(self):
        """绘制页眉"""
        current_time = datetime.datetime.now()
        date_text = current_time.strftime("%Y年%m月%d日 %A")
        
        # 日期标题
        title = self.title_font.render(date_text, True, self.COLORS['ACCENT'])
        title_rect = title.get_rect(center=(self.width/2, 80))
        self.surface.blit(title, title_rect)

    def draw_footer(self):
        """绘制页脚"""
        current_time = datetime.datetime.now()
        info_text = f"生成时间: {current_time.strftime('%H:%M:%S')}"
        
        info = self.content_font.render(info_text, True, self.COLORS['SECONDARY'])
        info_rect = info.get_rect(center=(self.width/2, self.height - 60))
        self.surface.blit(info, info_rect)

    def generate_wallpaper(self, content):
        """生成壁纸"""
        self.draw_background()
        self.draw_header()
        self.draw_text_content(content)
        self.draw_footer()
        
        # 保存图片
        pygame.image.save(self.surface, self.output_path)
        print(f"壁纸已生成: {self.output_path}")

def main():
    parser = argparse.ArgumentParser(description='简约壁纸生成器')
    parser.add_argument('--output', '-o', default='/home/w-dingyu01@uos/dy外网/My_model/pygame测试/wallpaper2.png',
                       help='输出文件路径')
    parser.add_argument('--width', '-W', type=int, default=1920,
                       help='壁纸宽度')
    parser.add_argument('--height', '-H', type=int, default=1080,
                       help='壁纸高度')
    parser.add_argument('--content', '-c', type=str, 
                       default='''这是一个简约的壁纸\n支持多行文本显示\n使用\\n进行换行\n保持简洁美观
                       test
                       1221
                       12312''',
                       help='壁纸文本内容，使用\\n换行')
    
    args = parser.parse_args()
    
    # 创建生成器并生成壁纸
    generator = SimpleWallpaperGenerator(args.output, args.width, args.height)
    generator.generate_wallpaper(args.content)

if __name__ == "__main__":
    main()